<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="68px" size="small">
      <el-form-item label="" prop="autoSheet">
        <el-select v-model="queryParams.autoSheet" class="labelWidth" clearable style="width: 170px"
                   placeholder="请选择是否自动派单">
          <el-option
            key="0"
            label="手动"
            value="0">
          </el-option>
          <el-option
            key="1"
            label="自动"
            value="1">
          </el-option>
        </el-select>
      </el-form-item>
      <!--      <el-form-item label="" prop="createSource">
              <el-input
                v-model="queryParams.createSource"
                clearable
                placeholder="请输入创建来源"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>-->
      <el-form-item label="" prop="enableSheet">
        <el-select v-model="queryParams.enableSheet" class="labelWidth" style="width: 170px" clearable
                   placeholder="请选择类型是否启用">
          <el-option
            key="0"
            label="禁用"
            value="0">
          </el-option>
          <el-option
            key="1"
            label="启用"
            value="1">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="" prop="faultCode">
        <el-input
          v-model="queryParams.faultCode"
          clearable
          placeholder="请输入关联故障码"
          @keyup.enter.native="handleQuery"
          style="width: 170px"
        />
      </el-form-item>
      <!--      <el-form-item label="" prop="sendToMasterConsumerId">
              <el-input
                v-model="queryParams.sendToMasterConsumerId"
                clearable
                placeholder="请输入主送人ID"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>-->
      <el-form-item label="" prop="sendToMasterConsumerName">
        <el-input
          v-model="queryParams.sendToMasterConsumerName"
          clearable
          placeholder="请输入主送人名称"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="sheetTypeName">
        <el-input
          v-model="queryParams.sheetTypeName"
          clearable
          placeholder="请输入类型名称"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="emailAddress">
        <el-input
          v-model="queryParams.emailAddress"
          clearable
          placeholder="请输入邮箱地址"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="enableEmail">
        <el-select v-model="queryParams.enableEmail" class="labelWidth" style="width: 170px" clearable
                   placeholder="请选择开启邮件通知">
          <el-option
            key="0"
            label="不开启"
            value="0">
          </el-option>
          <el-option
            key="1"
            label="开启"
            value="1">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--       <el-col :span="1.5">
         <el-button
           type="primary"
           plain
           icon="el-icon-plus"
           size="mini"
           @click="handleAdd"
           v-hasPermi="['business:workSheetType:add']"
         >新增</el-button>
       </el-col>
 <el-col :span="1.5">
         <el-button
           type="success"
           plain
           icon="el-icon-edit"
           size="mini"
           :disabled="single"
           @click="handleUpdate"
           v-hasPermi="['business:workSheetType:edit']"
         >修改</el-button>
       </el-col>
       <el-col :span="1.5">
         <el-button
           type="danger"
           plain
           icon="el-icon-delete"
           size="mini"
           :disabled="multiple"
           @click="handleDelete"
           v-hasPermi="['business:workSheetType:remove']"
         >删除</el-button>
       </el-col>-->
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['business:workSheetType:export']"
          icon="el-icon-download"
          plain
          size="mini"
          type="warning"
          @click="handleExport"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="workSheetTypeList" @selection-change="handleSelectionChange" highlight-current-row>
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column align="center" label="主键" prop="id"/>
      <el-table-column align="center" label="是否自动派单" prop="autoSheet">
        <template slot-scope="scope">
          <el-tag :type="scope.row.autoSheet==1?'success':'danger'">{{
              scope.row.autoSheet == 1 ? '自动' : '手动'
            }}
          </el-tag>
        </template>
      </el-table-column>
      <!--      <el-table-column align="center" label="创建来源" prop="createSource"/>-->
      <el-table-column align="center" label="类型是否启用" prop="enableSheet">
        <template slot-scope="scope">
          <el-tag :type="scope.row.enableSheet==1?'success':'danger'">{{
              scope.row.autoSheet == 1 ? '启用' : '禁用'
            }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="关联故障码" prop="faultCode">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" :content="scope.row.faultCodeDesc" placement="top">
            <el-tag>{{ scope.row.faultCode }}</el-tag>
          </el-tooltip>
          <!--          <el-tag>{{ scope.row.faultCode}}</el-tag>
                    <div>{{scope.row.faultCodeDesc}}</div>-->
        </template>
      </el-table-column>
      <el-table-column align="center" label="主送人ID" prop="sendToMasterConsumerId"/>
      <el-table-column align="center" label="主送人名称" prop="sendToMasterConsumerName"/>
      <el-table-column align="center" label="类型名称" prop="sheetTypeName"/>
      <el-table-column align="center" label="邮件地址" prop="emailAddress"/>
      <el-table-column align="center" label="邮件通知" prop="enableEmail">
        <template slot-scope="scope">
          <el-tag :type="scope.row.enableEmail==1?'success':'danger'">{{
              scope.row.enableEmail == 1 ? '启用' : '禁用'
            }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button
            :class="['text-button', { 'active': active===scope.$index + 'handleUpdate' }]"
            v-hasPermi="['business:workSheetType:edit']"
            icon="el-icon-edit"
            size="mini"
            type="text"
            @click="handleUpdate(scope.row),active=scope.$index + 'handleUpdate'"
          >修改
          </el-button>
          <!--          <el-button
                      v-hasPermi="['business:workSheetType:remove']"
                      icon="el-icon-delete"
                      size="mini"
                      type="text"
                      @click="handleDelete(scope.row)"
                    >删除
                    </el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />

    <!-- 添加或修改工单类型管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <div style="display: flex;flex-direction: row">
          <el-form-item label="是否自动派单" prop="autoSheet">
            <!--          <el-input v-model="form.autoSheet" placeholder="请输入是否自动派单"/>-->
            <el-switch
              v-model="form.autoSheet"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0">
            </el-switch>
          </el-form-item>
          <!--        <el-form-item label="创建来源" prop="createSource">
                    <el-input v-model="form.createSource" placeholder="请输入创建来源"/>
                  </el-form-item>-->
          <el-form-item label="类型是否启用" prop="enableSheet">
            <!--          <el-input v-model="form.enableSheet" placeholder="请输入开启邮件通知"/>-->
            <el-switch
              v-model="form.enableSheet"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0">
            </el-switch>
          </el-form-item>
        </div>

        <el-form-item label="关联故障码" prop="faultCode">
          <el-input v-model="form.faultCode" placeholder="请输入关联故障码" disabled/>
        </el-form-item>
        <el-form-item label="主送人查询" prop="sendToMasterConsumerId">
          <el-input v-model="form.sendToMasterConsumerPhone" placeholder="请输入主送人手机号码">
            <el-button slot="append" icon="el-icon-search" @click="submitFormSearch"></el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="主送人ID" prop="sendToMasterConsumerId">
          <el-input v-model="form.sendToMasterConsumerId" placeholder="请输入主送人ID" disabled/>
        </el-form-item>
        <el-form-item label="主送人名称" prop="sendToMasterConsumerName">
          <el-input v-model="form.sendToMasterConsumerName" placeholder="请输入主送人名称"/>
        </el-form-item>
        <el-form-item label="类型名称" prop="sheetTypeName">
          <el-input v-model="form.sheetTypeName" placeholder="请输入类型名称"/>
        </el-form-item>
        <div style="display: flex;flex-direction: row;">
          <el-form-item label="邮件通知" prop="enableEmail">
            <!--            <el-input v-model="form.enableEmail" placeholder="请输入${comment}"/>-->
            <el-switch
              v-model="form.enableEmail"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0">
            </el-switch>
          </el-form-item>
          <el-form-item label=" " prop="emailAddress" v-if="form.enableEmail==1">
            <el-input style="width: 280px;margin-left: -100px" v-model="form.emailAddress"
                      placeholder="请输入邮件地址"/>
          </el-form-item>
        </div>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listWorkSheetType,
  getWorkSheetType,
  delWorkSheetType,
  addWorkSheetType,
  updateWorkSheetType
} from "@/api/business/workSheetType";
import {isPhone} from '@/utils/validate'
import {searchUserPhone} from "@/api/business/specialConsumer";

export default {
  name: "WorkSheetType",
  data() {
    return {
      active: null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工单类型管理表格数据
      workSheetTypeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        autoSheet: null,
        createSource: null,
        enableSheet: null,
        faultCode: null,
        sendToMasterConsumerPhone: null,
        sendToMasterConsumerId: null,
        sendToMasterConsumerName: null,
        sheetTypeName: null,
        emailAddress: null,
        enableEmail: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        sendToMasterConsumerName: [
          {required: true, message: "主送人名称不能为空", trigger: "blur"}
        ],
        sheetTypeName: [
          {required: true, message: "类型名称不能为空", trigger: "blur"}
        ],
        emailAddress: [
          {required: true, message: "邮件地址不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    submitFormSearch() {
      if (!this.form.sendToMasterConsumerPhone) {
        this.$modal.msgError("请输入用户手机号");
        return false
      }
      if (!isPhone(this.form.sendToMasterConsumerPhone)) {
        this.$modal.msgError("请输入正确的手机号");
        return false
      }
      searchUserPhone(this.form.sendToMasterConsumerPhone).then(res => {
        let {consumerId, nickName} = res.data
        console.log(consumerId, nickName)
        this.form.sendToMasterConsumerId = consumerId
        this.form.sendToMasterConsumerName = nickName
      })
    },
    /** 查询工单类型管理列表 */
    getList() {
      this.loading = true;
      listWorkSheetType(this.queryParams).then(response => {
        this.workSheetTypeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        autoSheet: null,
        createBy: null,
        createSource: null,
        createTime: null,
        enableSheet: null,
        faultCode: null,
        sendToMasterConsumerId: null,
        sendToMasterConsumerName: null,
        sheetTypeName: null,
        updateBy: null,
        updateTime: null,
        emailAddress: null,
        enableEmail: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加工单类型管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getWorkSheetType(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改工单类型管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateWorkSheetType(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWorkSheetType(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除工单类型管理编号为"' + ids + '"的数据项？').then(function () {
        return delWorkSheetType(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/workSheetType/export', {
        ...this.queryParams
      }, `workSheetType_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
